package com.ruoyi.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.AnonymousAccess;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.domain.PaymentRecord;
import com.ruoyi.system.domain.RechargeRecord;
import com.ruoyi.system.domain.vo.ClientProductKeyListRes;
import com.ruoyi.system.domain.vo.ClientProductKeyListVo;
import com.ruoyi.system.domain.vo.ClientRechargeRecordListResVo;
import com.ruoyi.system.domain.vo.ClientRechargeRecordListVo;
import com.ruoyi.system.service.IPaymentRecordService;
import com.ruoyi.system.service.IRechargeRecordService;
import com.ruoyi.system.service.IVerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("/system/rechargeRecord")
@Api(value = "客户端充值记录")
public class RechargeRecordController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @Autowired
    private IRechargeRecordService rechargeRecordService;

    @Autowired
    private IPaymentRecordService paymentRecordService;

    @GetMapping("/list")
    @ApiOperation(value="客户端充值记录-查询", notes="客户端充值记录-查询")
    @AnonymousAccess
    public Result<?> list(ClientRechargeRecordListVo clientRechargeRecordListVo,
                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                          HttpServletRequest request) {
        String verifyEmail = null;
        try {
            verifyEmail = verifyCodeService.checkClientToken(request);
        } catch (Exception e) {
            Result.noauth(e.getMessage());
        }
        String email = null;
        if (Objects.nonNull(clientRechargeRecordListVo.getPaymentRecordId())) {
            PaymentRecord record = paymentRecordService.getById(clientRechargeRecordListVo.getPaymentRecordId());
            if (Objects.isNull(record)) {
                throw new ServiceException("订单不存在或者");
            }
            email = record.getBuyer();
        } else if (StrUtil.isNotBlank(clientRechargeRecordListVo.getBuyer())) {
            email = clientRechargeRecordListVo.getBuyer();
        }else {
            throw new ServiceException("请输入查询条件");
        }
        if (Objects.isNull(verifyEmail) || !StrUtil.equals(verifyEmail, email)) {
            throw new ServiceException("不能查询其他用户的信息");
        }

        IPage<ClientRechargeRecordListResVo> pages = rechargeRecordService.clientRechargeRecordList(new Page<ClientRechargeRecordListResVo>(pageNo, pageSize), clientRechargeRecordListVo);
        return Result.OK(pages);
    }
}
